﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_B_City
    {
        public List<T_B_City> GetList(string xml = "")
        {
            List<T_B_City> list = new List<T_B_City>();
            #region 获取数据
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetCity", null))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_City dto = new T_B_City();
                    dto.YID = dr.GetGuid(0);
                    dto.FID = dr.GetValue(1) is DBNull ? Guid.Empty : dr.GetGuid(1);
                    dto.YCode = dr.GetString(2);
                    dto.YName = dr.GetString(3);
                    dto.MLevel = dr.GetInt16(4);
                    dto.IsHasChild = dr.GetBoolean(5);
                    dto.IsRoot = dr.GetBoolean(6);
                    dto.IsLeaf = dr.GetBoolean(7);
                    dto.MSort = dr.GetDouble(8);
                    list.Add(dto);
                }
            }
            #endregion
            return list;
        }

        /// <summary>
        /// 操作数据
        /// </summary>
        /// <param name="xml">第一个为添加 ；第二个为修改；第三个为删除；</param>
        /// <returns></returns>
        public string Save(params IEnumerable<T_B_City>[] xml)
        {
            string addXmL = EFHelp<T_B_City>.GetXML(xml[0]);
            string editXML = EFHelp<T_B_City>.GetXML(xml[1]);
            string delXML = EFHelp<T_B_City>.GetXML(xml[2]);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@editXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@delXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = editXML;
            para[2].Value = delXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveCity", para);
            return (para[4].Value ?? string.Empty).ToString();
        }
    }
}
